// pages/order/index.js
const {
  post,
  get
} = getApp();
Page({
  data: {
    active: 0,
    data: [{
        params: {
          status: 0,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        showPage: false,
        dataList: []
      },
      {
        params: {
          status: 1,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        showPage: false,
        dataList: []
      },
      {
        params: {
          status: 2,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        showPage: false,
        dataList: []
      },
      {
        params: {
          status: 3,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        showPage: false,
        dataList: []
      },
      {
        params: {
          status: 4,
          page: 1,
          pageSize: 20
        },
        showLoading: true,
        showPage: false,
        dataList: []
      }
    ],
    orderType: {
      1: "待付款",
      2: "已支付",
      3: "已发货",
      4: "已收货",
      5: "已完成",
      6: "取消订单",
      7: "交易成功售后中",
      8: "交易成功部分售后中"
    }
  },
  // 初始化加载
  onLoad: function (e) {
    this.setData({
      active: parseInt(e.active)
    });
  },
  onShow: function () {
    this.setData({
      [`data[${this.data.active}].showLoading`]: true,
      [`data[${this.data.active}].dataList`]: [],
      [`data[${this.data.active}].params.page`]: 1,
      [`data[${this.data.active}].showPage`]: false
    });
    this.getList();
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  },
  // 切换内容
  changeActive(e) {
    this.setData({
      active: e.detail.index,
      [`data[${e.detail.index}].params.page`]: 1,
      [`data[${e.detail.index}].dataList`]: [],
      [`data[${e.detail.index}].showLoading`]: true,
      [`data[${e.detail.index}].showPage`]: false
    });
    this.getList();
  },
  // 获取数据
  getList() {
    if (this.data.data[this.data.active].showLoading) {
      post("/order/list", this.data.data[this.data.active].params).then(res => {
        if (res.result == 1) {
          this.setData({
            [`data[${this.data.active}].dataList`]: this.data.data[this.data.active].dataList.concat(res.data),
            [`data[${this.data.active}].showPage`]: true
          });
          if (res.data.length < this.data.data[this.data.active].params.pageSize) {
            this.setData({
              [`data[${this.data.active}].showLoading`]: false
            })
          }
        }
      })
    }
  },
  // 支付
  toPlay(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    get("/order/pay", {
      order_sn: sn
    }).then(res => {
      if (res.result == 1) {
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.packageValue,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success(res) {
            _this.setData({
              [`data[${_this.data.active}].params.page`]: 1,
              [`data[${_this.data.active}].dataList`]: [],
              [`data[${_this.data.active}].showLoading`]: true
            });
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
            _this.getList();
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    });
  },
  // 取消订单
  cancelOrder(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    wx.showModal({
      content: '您确定取消此订单吗',
      success(res) {
        if (res.confirm) {
          post('/order/cancel', {
            order_sn: sn,
            cancel_reason: ""
          }).then(res => {
            if (res.result == 1) {
              _this.setData({
                [`data[${_this.data.active}].params.page`]: 1,
                [`data[${_this.data.active}].dataList`]: [],
                [`data[${_this.data.active}].showLoading`]: true
              });
              _this.getList();
              wx.showToast({
                title: '取消订单成功',
                icon: 'success',
                duration: 2000
              });
            }
          })
        }
      }
    })
  },
  // 删除订单
  dedleteOrder(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    wx.showModal({
      content: '您确定删除此订单吗？',
      success(res) {
        if (res.confirm) {
          post('/order/delete', {
            order_sn: sn
          }).then(res => {
            if (res.result == 1) {
              _this.setData({
                [`data[${_this.data.active}].params.page`]: 1,
                [`data[${_this.data.active}].dataList`]: [],
                [`data[${_this.data.active}].showLoading`]: true
              });
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              });
              _this.getList();
            }
          })
        }
      }
    })
  },
  // 确认收货
  confirmOrder(e) {
    const {
      sn
    } = e.currentTarget.dataset;
    const _this = this;
    wx.showModal({
      content: '您确定收货此订单吗？',
      success(res) {
        if (res.confirm) {
          post('/order/confirm', {
            order_detail_sn: sn
          }).then(res => {
            if (res.result == 1) {
              _this.setData({
                [`data[${_this.data.active}].params.page`]: 1,
                [`data[${_this.data.active}].dataList`]: [],
                [`data[${_this.data.active}].showLoading`]: true
              });
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              });
              _this.getList();
            }
          })
        }
      }
    })
  },
  // 售后
  afterSales() {
    wx.makePhoneCall({
      phoneNumber: '110'
    })
  },
  // 详情
  goToMyOrder(e) {
    wx.navigateTo({
      url: `../myOrderDetail/index?sn=${e.currentTarget.dataset.sn}`,
    });
  },
  // 页面上拉触底事件的处理函数
  bindscrolltolower: function () {
    this.setData({
      [`data[${this.data.active}].params.page`]: this.data.data[this.data.active].params.page += 1
    });
    this.getList();
  }
})